<%@ page contentType="application/json;charset=UTF-8" language="java" %><%--
--%><%@ page import="java.util.Collection" %><%--
--%><%@ page import="o.json.JSONFactory" %><%--
--%><%@ page import="o.json.JSONValue" %><%--
--%><%@ page import="o.json.JSONWriter" %><%--
--%><%@ page import="com.google.appengine.api.users.User" %><%--
--%><%@ page import="com.google.appengine.api.users.UserService" %><%--
--%><%@ page import="com.google.appengine.api.users.UserServiceFactory" %><%--
--%><%@ page import="wwiii.AreaMap" %><%--
--%><%@ page import="wwiii.Player" %><%--
--%><%@ page import="wwiii.PlayerManager" %><%--
--%><%!

	public int parseInt(String value, int def) {
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {
			return def;
		}
	}
	public String notNull(String value, String def) {
		if (value != null && !value.equals("")) {
			return value;
		}
		return def;
	}

%><%

	final UserService us = UserServiceFactory.getUserService();
	if (!us.isUserLoggedIn()) {
		response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
		return;
	}
	final User user = us.getCurrentUser();

	final PlayerManager pm = new PlayerManager();

	String id   = user.getUserId();             // "id"+ Math.random();
	String name = request.getParameter("name"); // user.getNickname();
	String mail = user.getEmail();              // request.getParameter("mail");
	int    rank = parseInt(request.getParameter("rank"), 0);

	final Player player = new Player(id, mail, name, rank);
	pm.storePlayer(player);

	final Collection<Player> players = pm.loadPlayers();

	final JSONWriter writer = new JSONWriter();
	final JSONFactory factory = new JSONFactory();
	writer.write(factory.create(players));

%><%= writer.toString() %>
